import { Component, OnInit } from '@angular/core';
import { FundDetailService } from './fund-detail.service';
import { environment } from '../../../../../../../src/environments/environment';
import { AppPaginationComponent, PaginationModel } from "../../../../../widgets/pagination/pagination.component";
import * as _ from "lodash";
import { DataService } from 'app/services/data.service';

@Component({
  selector: 'app-fund-detail',
  templateUrl: './fund-detail.component.html',
  styleUrls: ['./fund-detail.component.scss'],
  providers: [FundDetailService]
})
export class FundDetailComponent implements OnInit {
  // lodash
  public _: any = _;
  // 菜单数据
  public menuData: any;
  // 菜单权限set
  public menuResourceSet: any = [];
  public listData: any = [];
  public item: any;
  //获取搜索条件字典值
  public dictionary: any = {
    QDII_RECORD_TYPE: [],//风控条目属性  
    QD_ACCOUNT_TYPE: [],//适用基金类别
  }

  //分页配置
  public listPageInfo = {
    currentPageNum: 1,
    pageSize: 50,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  //查询列表getBody
  public getBody: any = {
    pageIndex: 1,
    pageSize: 50,
    nFundNo: '',
    sItemType:'',
    sFileSource: '',
    sFundType:'',
    sFileName: '',
    f1: '',
  };

  // 导出
  exportExcel() {
    let that = this;
    let getBody={
      nFundNo : this.item.nFundNo,
      sItemType:this.getBody.sItemType,
      sFileSource:this.getBody.sFileSource,
      sFundType:this.getBody.sFundType,
      sFileName:this.getBody.sFileName,
      f1:this.getBody.f1
    }
    console.log(this.getBody);
    // console.log("===============");
    // let checkedItem = [];
    // _.forEach(this.listData, item => {
    //   if (item.isChecked) {
    //     checkedItem.push(item.nQdId);
    //   }
    // })
    // nQdIdList=checkedItem;
    // let converItem: any = checkedItem.join(',');
    // let getBody = {
    //   "nAuditNoList": converItem,
    //   "sFundType": this.annuityCheckBody.sFundType,
    //   "sFullName": this.annuityCheckBody.sFullName,
    //   "sFundCode": this.annuityCheckBody.sFundCode,
    //   "sModifyItem": this.annuityCheckBody.sModifyItem
    // }
    const iframe = window['$']("<iframe id='downloadiframe'>");
    iframe.attr('style', 'display:none');
    //  转码，兼容ie
    // getBody.sFullName = encodeURI(getBody.sFullName)
    iframe.attr('src', environment.server + '/pms/v1/riskmanage/qdiiData/export/exportFundEntry?token=' + sessionStorage.getItem('token') + '&nFundNo=' + getBody.nFundNo+'&sItemType='+getBody.sItemType+'&sFileSource='+getBody.sFileSource+'&sFundType='+getBody.sFundType+'&sFileName='+getBody.sFileName+'&f1='+getBody.f1);
    window['$']('body').append(iframe);
    setTimeout("$('#downloadiframe').remove()", 60000);
  }

  //列表全选
  public isCheckedAll: any = false;

  constructor(
    public fundDetailService: FundDetailService,
    public dataService: DataService

  ) { }

  ngOnInit() {
    let that = this;
    //通过服务获取字典值
    this.dictionary = JSON.parse(localStorage.getItem("dictionaryList"));
    this.loadlist();
    console.log(this.item);
    // 权限处理
    this.dataService.initTree.subscribe((data) => {
      if (data) {
        let SessionMenuData = sessionStorage.getItem('menu');
        this.menuData = JSON.parse(SessionMenuData)
        this.menuResourceSet = [];
        _.forEach(this.menuData, (item, index) => {
          if (item.sName == '风控管理') {        
            _.forEach(item.children, (it, idx) => {
              if (it.sName == 'QDII条目管理') {             
                _.forEach(it.children, (ite, i) => {
                  if (ite.sName == '基金条目') { 
                    that.menuResourceSet = ite.resourceSet
                    sessionStorage.setItem('btn-QDII', this.menuResourceSet);
                  }
                })
              }
            })
          }
        })      
      }
    })
    let SessionMenuData = sessionStorage.getItem('menu');
    this.menuData = JSON.parse(SessionMenuData)
    this.menuResourceSet = [];
    _.forEach(this.menuData, (item, index) => {
      if (item.sName == '风控管理') {
        _.forEach(item.children, (it, idx) => {
          if (it.sName == 'QDII条目管理') {
            _.forEach(it.children, (ite, i) => {
              if (ite.sName == '基金条目') { 
                that.menuResourceSet = ite.resourceSet
                sessionStorage.setItem('btn-QDII', this.menuResourceSet);
              }
            })
          }
        })
      }
    })
    console.info(this.menuResourceSet)
  }

  // 条数分页
  sizeChange(pageSize: number) {
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      this.loadlist();
    }
  }
  // 列表分页
  pageNavigation(currentPageNum: number) {
    this.listPageInfo.currentPageNum = currentPageNum;
    this.loadlist(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
  }

  //分页列表
  loadlist(pageNum?, pageSize?) {
    this.getBody.nFundNo = this.item.nFundNo;
    this.fundDetailService.list(this.getBody).subscribe(data => {
      if (data) {
        console.log(data);
        this.listData = data.list;
        this.listPageInfo.currentPageNum = data.pageNum;
        this.listPageInfo.totalPages = data.pages;
        this.listPageInfo.total = data.total;
        this.listPageInfo.startRow = data.startRow;
        this.listPageInfo.endRow = data.endRow;
      }
    })
  }
}
